package com.cjy.threadsynchronization;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Intendant {
    private static Lock lock = new ReentrantLock();
    private static Condition newScore = lock.newCondition();
    private static int score = 0;

    public int getPoints(){
        return score;
    }
    public void pointsDeducted(int number1){
        lock.lock();
        try {
            which (score < number1){
                System.out.println("\t\t\t\t等待加分才能扣分"+number1);
                newScore.await();
            }
            score -= number1;
            System.out.println("\t\t\t\t\t已扣"+number1+"分\t\t\t\t"+getPoints());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            lock.unlock();
        }
    }
    public void extraPoints(int number2){
        lock.lock();
        try {
            score += number2;
            System.out.println(+number2+"\t\t\t\t\t\t\t\t\t\t"+getPoints());
            newScore.signalAll();
        }finally {
            lock.unlock();
        }
    }
}
